const getUsersUrl = "https://ali.ikenway.net:18443/api/user/findAll"
const enableUsersUrl = "https://ali.ikenway.net:18443/api/user/enable"
const disableUsersUrl = "https://ali.ikenway.net:18443/api/user/disable"
const updateUserIdentityUrl = "https://ali.ikenway.net:18443/api/user/updateIdentity"

let token = sessionStorage.getItem("token")
let users;

function initUser() {
    fetch(getUsersUrl, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': token
        }
    }).then(res => {
        res.json().then(json => {
            alertMsg(json.msg)
            if (json.code == 200) {
                users = json.data;

                initUserList();
            } else if (json.code == 400) {
                alertMsg(json.msg);
                window.location.href = loginPageUrl;
            }
        })
    })
}

function initUserList() {
    let tableInnerHtml = "";
    for (let i = 0; i < users.length; i++) {
        let status = users[i].enable == true ? "启用" : "禁用";
        let identity;
        let selectorHtml = "";
        switch (users[i].identity) {
            case "CUSTOMER":
                identity = "顾客";
                selectorHtml = `
                     <option value="0">` + identity + `</option>
                     <option value="1">顾客</option>
                     <option value="2">店员</option>
                `;
                break;
            case "MANAGER":
                identity = "店员";
                selectorHtml = `
                     <option value="0">` + identity + `</option>
                     <option value="1">顾客</option>
                     <option value="2">店员</option>
                `;
                break;
            case "ROOT":
                identity = "管理员";
                selectorHtml = `
                     <option value="0">` + identity + `</option>
                `;
                break;
        }

        tableInnerHtml += `
            <tr>
                <th scope="row">` + i + `</th>   
                <td>` + users[i].userId + `</td>
                <td>` + users[i].name + `</td>
                <td>` + users[i].username + `</td>
                <td>
                    <select class="form-select mb-lg-3 me-3" aria-label="Default select example">
                        ` + selectorHtml + `
                    </select>
                </td>
                <td>` + status + `</td>
                <td>
                    <button type="button" class="btn btn-primary me-3  pt-sm-1 pb-sm-1 w-2" onclick="updateUser(` + i + `)">修改</button>
                    <button type="button" class="btn btn-primary me-3  pt-sm-1 pb-sm-1 w-2" onclick="enableUser(` + i + `)">启用</button>
                    <button type="button" class="btn btn-danger  pt-sm-1 pb-sm-1 w-2" onclick="disableUser(` + i + `)">停用</button>
                </td>
            </tr>
        `;
    }
    document.getElementById("table-body").innerHTML = tableInnerHtml;
}

function enableUser(index) {
    fetch(enableUsersUrl, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': token
        },
        body: JSON.stringify({
            "userIds": [users[index].userId]
        })
    }).then(res => {
        res.json().then(json => {
            alertMsg(json.msg)
        })
    })
}

function disableUser(index) {
    fetch(disableUsersUrl, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': token
        },
        body: JSON.stringify({
            "userIds": [users[index].userId]
        })
    }).then(res => {
        res.json().then(json => {
            alertMsg(json.msg)
        })
    })
}

function updateUser(index) {
    let selectors = document.querySelectorAll(".form-select");
    let identity;
    switch (selectors[index].options[selectors[index].selectedIndex].value) {
        case "0":
            identity = users[i].identity;
            break;
        case "1":
            identity = "CUSTOMER";
            break;
        case "2":
            identity = "MANAGER";
            break;
    }

    fetch(updateUserIdentityUrl, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': token
        },
        body: JSON.stringify({
            "userId": users[index].userId,
            "identity": identity
        })
    }).then(res => {
        res.json().then(json => {
            alertMsg(json.msg);
        })
    })
}